package com.app.fooddb.controller;

import com.app.fooddb.pojo.Foods;
import com.app.fooddb.pojo.Users;
import com.app.fooddb.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UsersController {
        @Resource
    public UsersService usersService;


    //登录
    @RequestMapping(value = "/app11/user/login",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> login(@RequestParam String telephone,@RequestParam String password){
        Map<String,Object> map = new HashMap<>();
        Users users = usersService.login(telephone,password);
        if(users != null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("list",users);
        }else {
            map.put("success",false);
            map.put("msg","失败");

        }
        return map;
    }
    //查询所有
    @RequestMapping(value = "/app13/food/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(){
        Map<String,Object> map = new HashMap<>();
        List<Foods> users = usersService.list();
        if(users != null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("list",users);
        }else {
            map.put("success",false);
            map.put("msg","失败");

        }
        return map;
    }

    //注册用户信息
    @RequestMapping(value = "/app12/user/register",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> regiter(Users users){
        Map<String,Object> map = new HashMap<>();
        int  register = usersService.register(users);
        if(register != 0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");

        }
        return map;
    }


    //根据id查询
    @RequestMapping(value = "/app14/food/findone",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findById(@RequestParam Integer id){
        Map<String,Object> map = new HashMap<>();
        Foods foods = usersService.findById(id);
        if(foods != null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("list",foods);
        }else {
            map.put("success",false);
            map.put("msg","失败");

        }
        return map;
    }
}
